//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CPerspectiveView.cpp>
///	@path	~/src/graphics/common/
///	@date	2008/11/01
///	@desc	.

#include "config/config.h"

#include "graphics/IDevice.h"
#include "graphics/RenderContext.h"
#include "graphics/dx9/import_dx9.h"
#include "graphics/common/CPerspectiveView.h"

namespace xeres {
	
	IMPL_IOBJECT_CLASS_DYNAMIC( CPerspectiveView );

	// CPerspectiveView
	CPerspectiveView::CPerspectiveView( void )
		: m_resX( 0 )
		, m_resY( 0 )
		, m_aspect( 1 )
		, m_fov( radians(90.f) )
		, m_near( 0 )
		, m_far( 100 )
	{
		m_projMatrix.identity();
		m_viewMatrix.identity();
	}
	// ~CPerspectiveView
	CPerspectiveView::~CPerspectiveView( void )
	{
	}
	// Reset
	void CPerspectiveView::Reset( RenderContext& rc )
	{
		m_resX = rc.ResX<float>();
		m_resY = rc.ResY<float>();
		m_aspect = m_resX / m_resY;
 		dx9::D3DXMatrixPerspectiveFovLH( (D3DXMATRIXA16*)&m_projMatrix, m_fov, m_aspect, m_near, m_far );
	}
	// GetTransform
	Matrix<float> CPerspectiveView::GetTransform( void )
	{
		return m_viewMatrix;
	}
	// GetProjection
	Matrix<float> CPerspectiveView::GetProjection( void )
	{
		return m_projMatrix;
	}
	// SetTransform
	void CPerspectiveView::SetTransform( const Matrix<float>& mat )
	{
		m_viewMatrix = mat;
	}
	// LookAt
	void CPerspectiveView::LookAt( const Vector<float>& eye , const Vector<float>& dir , const Vector<float>& up )
	{
		dx9::D3DXMatrixLookAtLH( (D3DXMATRIX*)&m_viewMatrix, (D3DXVECTOR3*)&eye, (D3DXVECTOR3*)&dir, (D3DXVECTOR3*)&up );
	}
	// SetProjection
	void CPerspectiveView::SetProjection( float fov , float near_clip , float far_clip )
	{
		m_fov = fov;
		m_near = near_clip;
		m_far = far_clip;

		D3DXMATRIXA16 matProj;
 		dx9::D3DXMatrixPerspectiveFovLH( (D3DXMATRIX*)&m_projMatrix, m_fov, m_aspect, m_near, m_far );
	}

	// GetFov
	float CPerspectiveView::GetFov( void ) const
	{
		return m_fov;
	}

	// SetFov
	void CPerspectiveView::SetFov( float fov )
	{
		SetProjection( fov , m_near , m_far );
	}

	// GetNear
	float CPerspectiveView::GetNear( void ) const
	{
		return m_near;
	}

	// SetNear
	void CPerspectiveView::SetNear( float near_clip )
	{
		SetProjection( m_fov , near_clip , m_far );
	}

	// GetFar
	float CPerspectiveView::GetFar( void ) const
	{
		return m_far;
	}

	// SetFar
	void CPerspectiveView::SetFar( float far_clip )
	{
		SetProjection( m_fov , m_near , far_clip );
	}
} // namespace xeres
